#!/usr/bin/env python
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from google.appengine.dist import use_library
use_library('django', '1.2')

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from model import QuestionM
from model import TopicM
from model import LectureM
from model import GeneralM
from model import ClassM

class ProfPage(webapp.RequestHandler):
    def get(self):
        lectureKey = self.request.get("lectureKey")
        if lectureKey:
            lecture=LectureM.getLectureByKey(lectureKey)
            topics = TopicM.getTopicsByLecture(lecture)
            
            if isinstance(topics, basestring): #Error
                self.redirect("/login")
            elif topics:
                viewVals=GeneralM.getViewVals()
                viewVals['lecture'] = lecture;
                viewVals['topics'] = map(TopicM.topic2Dict,TopicM.getTopicsByLecture(viewVals['lecture']))
                if(len(list(viewVals['topics'])) == 0):
                    viewVals['topics'] = None;
                
                viewVals['questions'] = QuestionM.getQuestionByLecture(viewVals['lecture'])
                if(len(list(viewVals['questions'])) == 0):
                    viewVals['questions'] = None;
                
                viewVals['lectureKey'] = lectureKey
                currTopic=ClassM.getCurrentTopic(lectureKey)
                if isinstance(currTopic, TopicM.Topic):
                    viewVals['currentTopic'] = str(currTopic.index - 1)
                # delete past unnecesary data
                self.response.out.write(template.render('view/prof.djhtml', viewVals))   
        else:
            self.redirect("/page_not_found")#add explanation

application = webapp.WSGIApplication([(r'.*', ProfPage)],debug=True)

def main():
    run_wsgi_app(application)
    
if __name__ == '__main__':
    main()
